const sqlOptions = require("../../sql");
const code = require("express").Router()

// 订单表订单状态
code.get("/order/status", (req, res) => {
    return res.status(200).send({
        code: 200,
        message: "",
        data: [
            {
                label: '0',
                value: '未付款'
            },
            {
                label: '1',
                value: '已付款'
            },
            {
                label: '2',
                value: '已发货'
            },
            {
                label: '3',
                value: '已签收'
            },
            {
                label: '4',
                value: '确认收货'
            },
            // 下面的没有用到应该
            {
                label: '5',
                value: '退货中'
            },
            {
                label: '6',
                value: '已退货'
            },
            {
                label: '7',
                value: '确认退货'
            },
        ]
    })
})

// 订单物流状态
code.get("/order/logistics", (req, res) => {
    return res.status(200).send({
        code: 200,
        message: "",
        data: [
            {
                label: '3',
                value: '待揽收'
            },
            {
                label: '4',
                value: '揽收中'
            },
            {
                label: '5',
                value: '运输中'
            },
            {
                label: '6',
                value: '中转中'
            },
            {
                label: '7',
                value: '清关中'
            },
            {
                label: '8',
                value: '发货到达'
            },
            {
                label: '9',
                value: '派送中'
            },
            {
                label: '10',
                value: '派送失败'
            },
            {
                label: '11',
                value: '派送异常'
            },
            {
                label: '12',
                value: '派送到达'
            },
            {
                label: '13',
                value: '签收'
            },
            {
                label: '0',
                value: '已下单'
            },
            {
                label: '1',
                value: '已发货'
            },
            {
                label: '2',
                value: '已出库'
            },
        ]
    })
})

// 收货表标签
code.get("/site/tag", (req, res) => {
    return res.status(200).send({
        code: 200,
        message: "",
        data: ['家', '公司', '学校', '父母', '朋友']
    })
})

// 地址级联接口
code.get("/address", async (req, res) => {
    try {
        if ("id" in req.query) {
            const {id} = req.query
            const dist = await req.db.query(sqlOptions({
                name: "rc_district",
                method: "list",
                data: {
                    pid: id
                }
            }))
            return res.status(200).send({
                code: 200,
                message: "",
                data: dist.map(item => ({
                    label: item.district,
                    value: item.district_id
                }))
            })
        }
        const dist = await req.db.query(sqlOptions({
            name: "rc_district",
            method: "list",
            data: {
                pid: 1
            }
        }))

        return res.status(200).send({
            code: 200,
            message: "",
            data: dist.map(item => ({
                label: item.district,
                value: item.district_id
            }))
        })
    } catch (e) {
        return res.status(500).send({
            code: 500,
            message: "系统错误",
            data: ""
        })
    }
})

module.exports = code